import axios,{InternalAxiosRequestConfig,AxiosError,AxiosResponse} from 'axios'
import {NETWORKTIMEOUT} from '@/utils/constant'
import {ResponseEnum} from '@/types/RabbitEnum'
import {getStorage} from '@/utils/storeutil'
import {TOKEKEY} from '@/utils/constant'
const service=axios.create({
    baseURL:import.meta.env.VITE_BASE_URL,
    timeout:NETWORKTIMEOUT
})
/**
 * 请求拦截器
 */
service.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
    config.headers['Content-Type']="application/x-www-form-urlencoded"
    const token=getStorage(TOKEKEY)
    if(token){
        config.headers['Authentication']=token
    }   
    return config
},(err:AxiosError)=>{
    return Promise.reject(err)
})

service.interceptors.response.use((response:AxiosResponse)=>{
    return response
},(err:AxiosError)=>{
    if(err.response){
        switch(err.response.status){
            case ResponseEnum.NotFound:
                //404
                break
            case ResponseEnum.ParamsValidate:
                //400
                break;
            case ResponseEnum.ServerError:
                 //400
                break;
            case ResponseEnum.TokenValidate:
                 //404
                break;
        }
    }
    return Promise.reject(err)
})
export default service